package org.datastealer.config;

import org.datastealer.properties.DataStealerProperties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

/**
 * Note：配置BeanFactory，并且增加扫描路径，扫描对应的路径
 *
 * @author ：mengjw
 * @description：Bean配置类
 * @date ：Created in 2020/8/24
 */
@PropertySource("classpath:datastealer-mysql.properties")
@PropertySource("classpath:datastealer-oracle.properties")
public class DataStealerBeanConfiguration implements BeanFactoryAware {

    private BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    @Bean
    public DataStealerBeanDefinitionScanner init(DataStealerProperties dataStealerProperties) {
        // 如果启用，则增加扫描
        if (dataStealerProperties.enable) {
            // 根据数据库类型构建扫描
            return new DataStealerBeanDefinitionScanner(
                    (BeanDefinitionRegistry) beanFactory, dataStealerProperties.dbType);
        }
        return null;
    }

}